<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\services\user;

use app\dao\user\UserBelongStoreDao;
use app\services\BaseServices;


/**
 * 用户归属门店
 * Class UserBelongStoreServices
 * @package app\services\user
 * @mixin UserBelongStoreDao
 */
class UserBelongStoreServices extends BaseServices
{

	/**
	 * 绑定类型名称
	 * @var string[]
	 */
	public $typeName = [
		'admin' => '管理员操作',
		'order' => '门店下单',
		'svip' => '开通会员卡',
		'spread' => '绑定推广人'
	];

    /**
     * UserBelongStoreServices constructor.
     * @param UserBelongStoreDao $dao
     */
    public function __construct(UserBelongStoreDao $dao)
    {
        $this->dao = $dao;
    }

	/**
	 * 记录用户归属门店关系
	 * @param int $uid
	 * @param int $store_id
	 * @param int $staff_id
	 * @param int $add_time
	 * @param int $admin_id
	 * @return bool
	 */
    public function setUserBelongStore(int $uid, int $store_id, string $type = 'order', int $staff_id = 0, int $add_time = 0, int $admin_id = 0)
    {
        if (!$uid || !$store_id) return false;
		/** @var UserServices $userServices */
		$userServices = app()->make(UserServices::class);
		$userInfo = $userServices->getUserInfo($uid);
		if (!$userInfo) {
			return false;
		}
		$status = false;
		switch ($type) {
			case 'admin'://管理员操作
				$status = true;
				break;
			case 'order'://下单
				$status = (bool)sys_config('belong_store_order', 1);
				break;
			case 'svip'://svip
				$status = (bool)sys_config('belong_store_svip', 1);
				break;
			case 'spread'://推广
				$status = (bool)sys_config('belong_store_spread', 1);
				break;
		}
		//没开启绑定归属
		if (!$status) {
			return false;
		}
		$data = ['type' => $type, 'uid' => $uid, 'store_id' => $store_id, 'staff_id' => $staff_id, 'add_time' => $add_time ?: time(), 'admin_id' => $admin_id];
		//验证是否换绑
		$changeBelongStatus =  sys_config('belong_store_change_svip', 0);
		if (!$userInfo['belong_store_id'] || ($type == 'svip' && $changeBelongStatus) || $type == 'admin') {//无绑定 || (开通付费会员类型 && 换绑开启) || 管理员手动操作
			if ($userInfo['belong_store_id']) {//换绑
				$data['group'] = 2;
			} else {
				$data['group'] = 1;
			}
			$this->dao->save($data);
			$userServices->update($uid, ['belong_store_id' => $store_id]);
			$userServices->cacheTag()->clear();
		}
		return true;
    }

	/**
	 * 获取绑定门店记录
	 * @param array $where
	 * @param string $field
	 * @param array $with
	 * @return array
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function getBelongStoreList(array $where, string $field = '*', array $with = [])
	{
		[$page, $limit] = $this->getPageValue();
		$list = $this->dao->getList($where, $field, $with, $page, $limit);
		$count = $this->dao->count($where);
		foreach ($list as &$item) {
			$item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
			$item['group_name'] = $item['group'] == 1 ? '绑定' : '换绑';
			$item['type_name'] = $this->typeName[$item['type']] ?? '未知类型';
		}
		return compact('list', 'count');
	}

}
